package com.zh.work.aop.jy;

import com.alibaba.fastjson.JSON;
import com.zh.common.entity.JSONResult;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 骏聿接口调用日志记录
 * @Author: Liard
 * @Create in: 2020/7/18 16:39
 * @Version: 1.0.1
 */
//@Aspect
//@Component
public class JyAop {

    private static final Logger log = LoggerFactory.getLogger(JyAop.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Pointcut(value = "execution(* com.zh.work.controller.out.jy.JyController.*(..))")
    public void JyAop(){}

    @Async
    @AfterReturning(value = "JyAop()", returning = "map")
    public void JyAopAfter(JoinPoint jp, Map<String, Object> map){
        Map<String, String> linkedHashMap = new LinkedHashMap<>();
        String sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        // 记录请求值
        MethodSignature methodSignature = (MethodSignature) jp.getSignature();
        Method method = methodSignature.getMethod();
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
        linkedHashMap.put("_id", UUID.randomUUID().toString());
        linkedHashMap.put("method", method.getName());
        linkedHashMap.put("param", jp.getArgs()[0].toString());
        linkedHashMap.put("addTime", sdfTime);
        linkedHashMap.put("notes", apiOperation.notes());
        log.info("###### 调用骏聿接口 -> {}", method.getName());
        mongoTemplate.save(linkedHashMap, "jy_api");
    }
}
